//
//  RoomScheduleView.m
//  mUBB
//
//  Created by Cosmin Stirbu on 3/16/13.
//  Copyright (c) 2013 Cosmin Stirbu. All rights reserved.
//

#import "ScheduleBackgroundView.h"
#import "UIViewAdditions.h"
#import "Utils.h"
#import "ConfigurationManager.h"



@implementation ScheduleBackgroundView

- (id)init {
    self = [super init];
    if (self) {
        // Set frame
        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 15.5 * VERTICAL_DIFF + 2 * VERTICAL_OFFSET);
        self.autoresizingMask = UIViewAutoresizingNone;
        // Set Times
        _times=[[NSArray alloc]
                initWithObjects:
                @"07:00",
                @"08:00",
                @"09:00",
                @"10:00",
                @"11:00",
                @"12:00",
                @"13:00",
                @"14:00",
                @"15:00",
                @"16:00",
                @"17:00",
                @"18:00",
                @"19:00",
                @"20:00",
                @"21:00",
                @"22:00",
                nil ];
        // Colors
        backgroundColor = [Utils colorWithHexString:[[ConfigurationManager instance] UIConfigValueForKey:@"schedule_background"]];
        
        // Times and lines
        timeAndLinesColor = [Utils colorWithHexString:[[ConfigurationManager instance] UIConfigValueForKey:@"times_and_lines_color"]];
        
        self.backgroundColor = backgroundColor;
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code 
    UIFont *timeFont=[UIFont systemFontOfSize:FONT_SIZE];
    for (NSInteger i=0;i<[_times count];i++) {
        NSString *time=[_times objectAtIndex:i];
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGRect timeRect=CGRectMake(HORIZONTAL_OFFSET, VERTICAL_OFFSET + i*VERTICAL_DIFF, TIME_WIDTH, FONT_SIZE+4.0);
        CGContextSetFillColorWithColor(context,timeAndLinesColor.CGColor);
        [time drawInRect:CGRectIntegral(timeRect) withFont:timeFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentRight];
        CGContextSetLineDash(context, 0,0,0);
        CGContextSetStrokeColorWithColor(context, timeAndLinesColor.CGColor);
        CGContextSetLineWidth(context,0.5);
        CGContextBeginPath(context);
        CGContextMoveToPoint(context, HORIZONTAL_OFFSET+TIME_WIDTH+HORIZONTAL_DIFFERENCE, VERTICAL_OFFSET+ i * VERTICAL_DIFF + roundf((FONT_SIZE + 4.0) / 2.0));
        CGContextAddLineToPoint(context, self.width, VERTICAL_OFFSET+ i * VERTICAL_DIFF + roundf((FONT_SIZE + 4.0) / 2.0));
        CGContextStrokePath(context);
        CGContextBeginPath(context);
        CGFloat dash[] = {2.0, 2.0};
        CGContextSetLineDash(context, 0.0, dash, 2);
        CGContextMoveToPoint(context, HORIZONTAL_OFFSET+TIME_WIDTH+HORIZONTAL_DIFFERENCE, VERTICAL_OFFSET+ i * VERTICAL_DIFF + roundf((FONT_SIZE + 4.0) / 2.0)+VERTICAL_DIFF/2);
        CGContextAddLineToPoint(context, self.width, VERTICAL_OFFSET+ i * VERTICAL_DIFF + roundf((FONT_SIZE + 4.0) / 2.0) + VERTICAL_DIFF/2);
        CGContextStrokePath(context);
        
    }
    
}


@end
